if (window.plus) {
    messagePageReady();
} else {
    document.addEventListener('plusready', messagePageReady, false);
}

$(function () {
    if (AGENT_CLIENT.WEB) {
        messagePageReady();
    }
})

// 初始化方法
function messagePageReady() {
    // IScroll
    // _loadingStep: 加载状态0默认，1显示加载状态，2执行加载数据，只有当为0时才能再次加载，这是防止过快拉动刷新
    var _myScroll, _pullDownEl, _pullDownL, _pagecount = 1, _loadingStep = 0;

    var messagePage = function () {
        this.init();
    };
    messagePage.prototype = {
        // 初始化页面
        init: function () {
            this.loadData();
            this.initIscroll();
            this.bindEvent();

        },
        // 加载数据
        loadData: function () {
            var model = this;
            // 3.新闻播报 4.促销活动
            var postdata = {index: 1, size: 20, search: {type: "3|4"}, sorting: {time: "DESC"}};
            $.ajax({
                headers: {'Content-Type': 'application/json'},
                type: "POST",
                url: CONFIG_MANAGER.RequstUrl + "v1/news/lists",
                data: JSON.stringify(postdata)
            }).done(function (res) {
                var newslist = JSON.parse(res).data;
                model.paintPage(newslist);
                // 最后查看日期
                setLocalStorage(STORAGE_MANAGER.lastCheckTime, new Date().getTime());
            });
        },

        // 画页面
        paintPage: function (newslist) {
            var html = [];
            var itemwidth = $(window).width() * 0.8;
            if (!newslist || newslist.length == 0) {
                $("#newslistdiv").html("<div id='nodatadiv' style='text-align: center;' class='zhanwushuju'><img src='../../images/zanwushuju.png'><br>暂无数据</div>");
                $("#newslistdiv").height($(window).height() + 10);
                _myScroll.refresh();
                _pullDownEl.hide();
                return;
            }
            $.each(newslist, function (i, item) {
                var create_time = new Date(toDateString(item.create_time, "yyyy-MM-dd hh:mm:ss")).getTime();
                var content = $("<label></label>").html(item.content);
                var isdisplayMore = (content.width() > itemwidth) ? "" : "style='display:none '";
                if (item.type == 4) {
                    html.push("<div class='xxggbg mbbottom'><div class='xgtb'><img src='../../images/ggtb2.png' /></div><div class='xgny'>");
                    html.push("<p class='xgnybt'><span>" + dateDiff(create_time) + "</span>促销活动</p>");

                    html.push("<p class='xgnynr'><b><label>" + item.content + "</label><span name='btnshow' " + isdisplayMore + "><a>展开详情</a></span></b></p>");
                    html.push("</div></div>");
                } else {
                    html.push("<div class='xxggbg mbbottom'><div class='xgtb'><img src='../../images/ggtb1.png' /></div><div class='xgny'>");
                    html.push("<p class='xgnybt'><span>" + dateDiff(create_time) + "</span>新闻播报</p>");
                    html.push("<p class='xgnynr'><b>" + item.content + "</b> <span name='btnshow' " + isdisplayMore + "><a>展开详情</a></span></p>");
                    html.push("</div></div>");
                }
            });

            $("#newslistdiv").html(html.join(""));
            var blankHight = ($(window).height() - CONFIG_MANAGER.headerHight) - $("#newslistdiv").height();
            if (blankHight > 0) {
                $("#newslistdiv").height($("#newslistdiv").height() + blankHight + 10);
            }
            _myScroll.refresh();
            _pullDownEl.hide();
        },

        // 绑定事件
        bindEvent: function () {
            // 置顶
            $("#btnback").on("click", function () {
                back();
            });

            // 展开/收起
            $("#newslistdiv").on("click", "span[name=btnshow] a", function () {
                var className = "";
                if ($(this).text() == "展开详情") {
                    // 展开
                    $(this).parents("p")[0].className = "zhankai";
                    $(this).text("收起详情");
                } else {
                    // 收起
                    $(this).parents("p")[0].className = "xgnynr";
                    $(this).text("展开详情");
                }
            });
        },

        // 初始化Iscroll
        initIscroll: function () {
            var model = this;
            _pullDownEl = $('#pullDown');
            _pullDownL = _pullDownEl.find('.pullDownLabel');

            _myScroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                click: true,
                disableMouse: true,
                disablePointer: true
            });
            //滚动时
            _myScroll.on('scroll', function () {
                if (_loadingStep == 0 && !_pullDownEl.attr('class').match('flip|loading')) {
                    if (this.y > 5) {
                        //下拉刷新效果
                        _pullDownEl.attr('class', "flip")
                        _pullDownEl.show();
                        _myScroll.refresh();
                        _pullDownEl.addClass('flip');
                        _pullDownL.html('松开刷新');
                        _loadingStep = 1;
                    } else if (this.y < (this.maxScrollY - 5)) {
                        _loadingStep = 1;
                    }
                }
            });
            //滚动完毕
            _myScroll.on('scrollEnd', function () {
                if (_loadingStep == 1) {
                    if (_pullDownEl.attr('class').match('flip|loading')) {
                        _pullDownEl.removeClass('flip').addClass('loading');
                        _pullDownL.html('加载中...');
                        _loadingStep = 2;
                        pullDownAction();
                    }
                }
            });

            // 下拉刷新
            function pullDownAction() {
                $("#newslistdiv").html("");
                setTimeout(function () {
                    _pagecount = _pagecount + 1;
                    model.loadData();
                    _pullDownEl.removeClass('loading');
                    _pullDownL.html('下拉刷新');
                    _myScroll.refresh();
                    _loadingStep = 0;
                }, 1000);
            }

            document.addEventListener('touchmove', function (e) {
                e.preventDefault();
            }, false);
        }
    };

    var _messagePage = new messagePage();
}
